package org.dromara.common.json.config;

import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializerBase;

import java.math.BigDecimal;

public class BigDecimalSerializer extends ToStringSerializerBase
{
    /**
     * Singleton instance to use.
     */
    public final static BigDecimalSerializer instance = new BigDecimalSerializer();

    /**
     *<p>
     * Note: usually you should NOT create new instances, but instead use
     * {@link #instance} which is stateless and fully thread-safe. However,
     * there are cases where constructor is needed; for example,
     * when using explicit serializer annotations like
     * {@link com.fasterxml.jackson.databind.annotation.JsonSerialize#using}.
     */
    public BigDecimalSerializer() { super(BigDecimal.class); }


    @Override
    public final String valueToString(Object value) {

        if (value instanceof BigDecimal) {
            return ((BigDecimal) value).toPlainString();

        }

        return value.toString();
    }
}
